package cn.zwxky.android.staff.Login;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.LsfbAppManager;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.SPUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import cn.zwxky.android.staff.IndexActivity2;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.NoDoubleClickUtils;
import cn.zwxky.android.staff.Utils.Permission;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * 登录页面
 * Created by Administrator on 2017/3/28 0028.
 */
@ContentView(R.layout.aty_login)
public class LoginActivity extends LsfbActivity {

    /**
     * 账号
     */
    @ViewInject(R.id.aty_login_et_phone)
    private EditText et_phone;

    /**
     * 密码
     */
    @ViewInject(R.id.aty_login_et_pwd)
    private EditText et_pwd;

    private String username = "";
    private String password = "";

    @Override
    public void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        username = String.valueOf(SPUtils.get(this, "et_username", ""));
        password = String.valueOf(SPUtils.get(this, "et_password", ""));
        et_phone.setText(username);
        et_pwd.setText(password);
        Permission permission = new Permission(this);
        permission.GetMeshPermission();
        permission.GetLocationPermission2();

        String userid= (String) SPUtils.get(getApplicationContext(),"userid1","-1");

        if (!userid.equals("-1")) {
            T.showShort(this,"自动登录中..");
            login();
        }


    }

    @Override
    public void initEvent() {

    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_login_btn_login, R.id.aty_login_btn_forget})
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.aty_login_btn_login:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_phone.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_pwd.getWindowToken(), 0);
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    login();
                }
                break;
            //忘记密码
            case R.id.aty_login_btn_forget:
                startActivity(new Intent(this, ForgetActivity.class));
                break;

        }
    }


    @Override
    public void initData() {

    }

    public void login() {
        if (et_phone.getText().toString().isEmpty()) {
            SN.SnackShow(et_pwd, "请输入账号");
            return;
        }
        if (et_pwd.getText().toString().isEmpty()) {
            SN.SnackShow(et_pwd, "请输入密码");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("account", et_phone.getText().toString());
        hashMap.put("pwd", MD5.GetMD5Code(et_pwd.getText().toString()));
        hashMap.put("rid", JPushInterface.getRegistrationID(getApplicationContext()));

        new BaseInternet().getData(URLString.LOGIN, hashMap, LoginBean.class, new LoginEvent<LoginBean>(), false);
    }

    @EventAnnotation
    public void LoginEvent(LoginEvent<LoginBean> event) {
        switch (event.getCode()) {
            case 200:
//                startActivity(new Intent(this, IndexActivity.class));
                Config.setUserId(event.getData().getToken());
                LittleUtils.saveuser(Config.getUserId(), getApplicationContext());
                LittleUtils.nodel=event.getData().getData().getNodel();
                LittleUtils.job_yw=event.getData().getData().getJob_yw();
                LittleUtils.depart=event.getData().getData().getDepart();
                LittleUtils.depart_yw=event.getData().getData().getDepart_yw();
                SPUtils.put(this, "et_username", et_phone.getText().toString());
                SPUtils.put(this, "et_password", et_pwd.getText().toString());
                startActivity(new Intent(this, IndexActivity2.class));
//                T.showShort(this, event.getMsg());
                break;
            default:
                SN.SnackShow(et_phone, event.getMsg());
                break;
        }
    }

    //点击退出弹出确认提示框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dialog();
            return true;
        }
        return true;
    }

    protected void dialog() {

//    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//    设置Title的图标
//        builder.setIcon(R.mipmap.ic_launcher);
//    设置Title的内容
        builder.setTitle("温馨提示");
//    设置Content来显示一个信息
        builder.setMessage("确定要退出应用吗？");
//    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
                LsfbAppManager.getAppManager().finishAllActivity();
                System.exit(0);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
//    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
//    显示出该对话框
        builder.show();
    }

}
